package com.zcx.qshop.action;

import android.content.Context;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.conn.JsonLoginAsyGet;
import com.zcx.qshop.conn.JsonUserAsyGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/27 0027.
 */
public class LoginAction {

    private List<OnLoginStateListener> list = new ArrayList<>();

    private JsonUserAsyGet.Info info;

    private JsonUserAsyGet jsonUserAsyGet;

    public LoginAction(final Context context){

        jsonUserAsyGet = new JsonUserAsyGet(QSApplication.QSPreferences.readUid(), new AsyCallBack<JsonUserAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonUserAsyGet.Info info) throws Exception {

                LoginAction.this.info = info;

                loginState();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                jsonUserAsyGet.execute(context, false);

            }

        });

        if(!jsonUserAsyGet.uid.equals("")){

            info = new JsonUserAsyGet.Info();

            info.uid = jsonUserAsyGet.uid;

            jsonUserAsyGet.execute(context, false);

        }

    }

    public void login(final Context context, String mobile, String password, final OnLoginListener onLoginListener){

        new JsonLoginAsyGet(mobile, password, new AsyCallBack<String>(){

            @Override
            public void onStart(int type) throws Exception {

                onLoginListener.onStart();

            }

            @Override
            public void onSuccess(String toast, int type, String s) throws Exception {

                QSApplication.QSPreferences.saveUid(s);

                info = new JsonUserAsyGet.Info();

                jsonUserAsyGet.uid = info.uid = s;

                jsonUserAsyGet.execute(context, false);

                onLoginListener.onSuccess(s);

                loginState();

            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                onLoginListener.onFail();

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

                onLoginListener.onEnd();

            }

        }).execute(context);

    }

    public void setPicurl(String picurl){

        info.picurl = picurl;

        loginState();

    }

    public void setName(String name){

        info.name = name;

        loginState();

    }

    public void exit(){

        info = null;

        QSApplication.QSPreferences.saveUid("");

        QSApplication.ShoppingAction.clearData();

        loginState();

    }

    public void addOnLoginStateListener(OnLoginStateListener onLoginStateListener){

        list.add(onLoginStateListener);

        loginState();

    }

    public void removeOnLoginStateListener(OnLoginStateListener onLoginStateListener){

        list.remove(onLoginStateListener);

    }

    private void loginState(){

        for (int i = 0 ; i < list.size() ; i ++){

            if(info == null){

                list.get(i).onExit();

            }else{

                list.get(i).onLogin(info);

            }

        }

    }

    public boolean isLogin(){

        return info != null;

    }

    public static class OnLoginStateListener{

        public void onLogin(JsonUserAsyGet.Info info){};

        public void onExit(){};

    }

    public static class OnLoginListener{

        public void onStart(){};

        public void onSuccess(String uid){};

        public void onFail(){};

        public void onEnd(){};

    }

}
